// pagesMine/pages/personalDetails/personalDetails.js
import ajax from "../../../utils/index"
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    version: '',
    gardenRoleItem: null,
    pageRoles: 0
  },
  async getUserInfo() {
    console.log(this.data.gardenRoleItem)
    const gardenId = this.data.gardenRoleItem?.id || ''
    const res = await ajax.get(`/wx/user/garden/info?gardenId=${gardenId}`)
    // const userInfo = await ajax.getUserInfo()
    const pageRoles = wx.getStorageSync('pageRoles') || 0;
    this.setData({ userInfo: res.data.user, pageRoles })
  },
  exitEvent() {
    wx.showModal({
      title: '提示',
      content: '确定要退出小程序吗?',
      success: (res) => {
        if (!res.confirm) return
        wx.clearStorageSync()
        wx.removeStorageSync('Token')
        wx.removeStorageSync('userInfo')
        wx.removeStorageSync('pageRoles')
        wx.removeStorageSync('gardenRoleItem')
        wx.switchTab({ url: '/pages/index/index' })
      }
    })
  },
  /**
   * 园区及权限列表
   */
  async getGardenRole() {
    if (this.data.gardenRoleItem) return this.getUserInfo()
    let { gardenRoleItem } = this.data
    const res = await ajax.get('/gardenRole/list');
    gardenRoleItem = res.rows[0]
    this.setData({ gardenRoleItem })
    this.getUserInfo()
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const gardenRoleItem = wx.getStorageSync('gardenRoleItem') || null
    this.setData({ version: app.globalData.version, gardenRoleItem })
    this.getGardenRole()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})